package com.example.demo1;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class UserActivity extends AppCompatActivity implements View.OnClickListener {
    TextView textView;
    private EditText edit_username;
    private EditText edit_phone;
    private EditText edit_pwd;
    private EditText edit_email;
    User user = new User();
    private Button button_change;
    private Button button_logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        initView();
        Intent intent = getIntent();

        user.setName(intent.getStringExtra("username"));
        user.setPhone(intent.getStringExtra("phone"));
        user.setPwd(intent.getStringExtra("pwd"));
        user.setEmail(intent.getStringExtra("email"));
        edit_username.setText(user.getName());
        edit_phone.setText(user.getPhone());
        edit_pwd.setText(user.getPwd());
        edit_email.setText(user.getEmail());

    }

    private void initView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
        edit_pwd = (EditText) findViewById(R.id.edit_pwd);
        edit_email = (EditText) findViewById(R.id.edit_email);
        button_change = (Button) findViewById(R.id.button_change);
        button_logout = (Button) findViewById(R.id.button_logout);

        button_change.setOnClickListener(this);
        button_logout.setOnClickListener(this);
        button_change = (Button) findViewById(R.id.button_change);
        button_change.setOnClickListener(this);
        button_logout = (Button) findViewById(R.id.button_logout);
        button_logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_change:
                String username = edit_username.getText().toString().trim();
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(this, "username不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String phone = edit_phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(this, "phone不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String pwd = edit_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, "pwd不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String email = edit_email.getText().toString().trim();
                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(this, "email不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                user.setName(username);
                user.setPhone(phone);
                user.setPwd(pwd);
                user.setEmail(email);
                user.updateBySQLite(this);
                edit_username.setText(user.getName());
                edit_phone.setText(user.getPhone());
                edit_pwd.setText(user.getPwd());
                edit_email.setText(user.getEmail());
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_logout:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    }


}

